<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['product']) && ($_SESSION['product']&1<<1)==0)) {
	Vfriend_Function::redirect(HOST_BACKEND);
}
$strRefer	 	= $objRequest->getParam('prefer',HOST_BACKEND . 'product/index');
$strName 		= $objRequest->getParam('pname','');
$strDescription = $objRequest->getParam('pdescription','');
$intCategoryId 	= $objRequest->getParam('pcategoryid',0);
$intSubCateId 	= $objRequest->getParam('psubcategoryid',0);
$intPrice 		= $objRequest->getParam('pprice',0);
$intPriceSale 	= $objRequest->getParam('pprice_sale',0);
$intDiscount 	= $objRequest->getParam('pdiscount',0);
$intValue	 	= $objRequest->getParam('pvalue',0);
$intQuantity 	= $objRequest->getParam('pquantity',0);
$intHarvest	 	= $objRequest->getParam('pharvest',0);
$fImage	 		= $_FILES['pimage'];
$fImageExp	 	= $_FILES['pimage_exp'];
$intLimited	 	= $objRequest->getParam('plimited',0);
$intType	 	= $objRequest->getParam('ptype',0);
$intHot		 	= $objRequest->getParam('phot',0);
$intDisOrder 	= $objRequest->getParam('pdisorder',0);
$strImage		= '';
$strImageExp	= '';
$intError		= 0;
if($intCategoryId == 0) $intError = 2;
elseif($intSubCateId == 0) $intError = 3;
elseif($strName == '') $intError = 4;
elseif($strName != '' && strlen($strName)<3) $intError = 5;
elseif($intPrice == 0) $intError = 6;
elseif($intPriceSale == 0) $intError = 7;
elseif($intValue == 0) $intError = 8;
elseif(isset($fImage['name']) && $fImage['name'] == '') $intError = 9;
elseif(isset($fImageExp['name']) && $fImageExp['name'] == '') $intError = 11;
if($intError == 0) {
	$arrImage 		= Globals::uploadProduct($fImage);
	$arrImageExp 	= Globals::uploadProduct($fImageExp);
	if($arrImage['error'] == 0) {
		$strImage	= $arrImage['name'];
	} else {
		$intError	= 10;
	}
	if($arrImageExp['error'] == 0) {
		$strImageExp	= $arrImageExp['name'];
	} else {
		$intError		= 11;
	}
}
if($intError == 0) {
	if(isset($_SESSION['ppost'])) unset($_SESSION['ppost']);
	if(isset($_SESSION['perror'])) unset($_SESSION['perror']);
	$objProduct = new Product();	
	$intError	= $objProduct->add($AppUI->id, $strName, $strDescription, $intCategoryId, $intSubCateId,
									$intPrice, $intPriceSale, $intDiscount, $intValue, $intHarvest,
									$strImage, $strImageExp, $intLimited, $intType, $intHot, $intDisOrder,
									$intQuantity);
	if($intError == 0) {
		Vfriend_Function::setMsg($arrMessage[7]);
		Vfriend_Function::redirect($strRefer);
	}
}
if($intError > 0) {
	$_SESSION['ppost']	= $objRequest->getParams();
	$_SESSION['perror']	= $arrError[$intError];
	Vfriend_Function::redirect(HOST_BACKEND . 'product/add?cate=' . $intCategoryId . '&sub=' . $intSubCateId);
}
?>